package com.aurora.color.adapter;

import static android.graphics.Color.parseColor;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.aurora.color.R;
import com.aurora.color.bean.Color;

import java.util.List;


public class FuListAdapter extends BaseAdapter {

    private List<Color> colorList;    //颜色列表
    private Context context;      //上下文内容


    /**
     * 带参构造
     */
    public FuListAdapter(List<Color> colorList, Context context) {
        this.colorList = colorList;
        this.context = context;
    }


    @Override
    public int getCount() {
        return colorList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint({"ViewHolder", "InflateParams"})
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.item_list, null);

        // 获取控件对象
        TextView tv_name = view.findViewById(R.id.item_color_name);
        TextView tv_code = view.findViewById(R.id.item_color_code);
        CardView cr_color = view.findViewById(R.id.item_card);

        String name = colorList.get(i).getFColor()[0];
        String code = colorList.get(i).getFColor()[1];
        int color = parseColor(code);    //将十六进制颜色转换成整型

        tv_name.setText(name);
        tv_code.setText(code);
        cr_color.setCardBackgroundColor(color);


        return view;
    }
}
